package scottb.flex;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class FlexButton extends JButton implements FlexLayout {

    // States
    private boolean up = true;
    private boolean over = false;
    private boolean down = false;

    // Layout and position
    private Integer top = null;
    private Integer left = null;
    private Integer bottom = null;
    private Integer right = null;
    private Integer horizontalCenter = null;
    private Integer verticalCenter = null;
    private Integer width = null;
    private Integer height = null;

    /**
     * Customize by:
     *
     * Font
     * FontColor
     * Border
     * Fill
     */


    public FlexButton() {

        addMouseListener(new MouseListener());
    }

    public FlexButton(String label) {
        super(label);

        addMouseListener(new MouseListener());
    }

    // Layout and position
    public FlexButton top(Integer top) {
        this.top = top;
        return this;
    }

    public Integer top() {
        return top;
    }

    public FlexButton left(Integer left) {
        this.left = left;
        return this;
    }

    public Integer left() {
        return left;
    }

    public FlexButton right(Integer right) {
        this.right = right;
        return this;
    }

    public Integer right() {
        return right;
    }

    public FlexButton bottom(Integer bottom) {
        this.bottom = bottom;
        return this;
    }

    public Integer bottom() {
        return bottom;
    }

    public FlexButton horizontalCenter(Integer center) {
        this.horizontalCenter = center;
        return this;
    }

    public Integer horizontalCenter() {
        return this.horizontalCenter;
    }

    public FlexButton verticalCenter(Integer center) {
        this.verticalCenter = center;
        return this;
    }

    public Integer verticalCenter() {
        return this.verticalCenter;
    }

    public FlexButton center() {
        verticalCenter(0);
        horizontalCenter(0);
        return this;
    }

    public FlexButton width(Integer width) {
        this.width = width;
        return this;
    }

    public Integer width() {
        return width;
    }

    public FlexButton height(Integer height) {
        this.height = height;
        return this;
    }

    public Integer height() {
        return height;
    }

    public Dimension getPreferredSize() {

        int width;
        int height;
        Dimension preferredSize = null;

        if(null != width()) {
            width = width();
        }
        else {

            preferredSize = super.getPreferredSize();
            width = preferredSize.width;
        }

        if(null != height()) {
            height = height();
        }
        else {

            if(null == preferredSize) {
                preferredSize = super.getPreferredSize();
            }

            height = preferredSize.height;
        }

        return new Dimension(width, height);
    }

    class MouseListener extends MouseAdapter {

        public void mouseEntered(MouseEvent event) {
            up = false;
            over = true;
            down = false;

            if(!isEnabled()) {
		        setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
        }

        public void mouseExited(MouseEvent event) {
            up = true;
            over = false;
            down =false;

            if(!isEnabled()) {
		        setCursor(Cursor.getDefaultCursor());
            }
        }

        public void mousePressed(MouseEvent event) {
            up = false;
            over = false;
            down = true;
        }

        public void mouseReleased(MouseEvent event) {
            up = false;
            over = true;
            down = false;
        }
    }
}
